<?php 
/***
	Jpaging.1.0.php
	name:xiaojia
	email:iatt@qq.com
	qq:273142650
	time:2011.9.9 15:21
***/

	class Jpaging {
		
		private $total; //总条数
		private $top; //每页显示条数
		private $page; //当前页
		private $show; //导航长度
		
		private $list_page;
		private $list_page_now;
		private $home_page;
		private $up_page;
		private $down_page;
		private $last_page;
		
		private $style;
		private $sql;
		
		public function init ($array) {
			$this -> total = $array['total'];
			$this -> top = $array['top'];
			$this -> page = $array['page'];
			$this -> show = $array['show'];
			$this -> list_page = $array['list_page'];
			$this -> list_page_now = $array['list_page_now'];
			$this -> home_page = $array['home_page'];
			$this -> up_page = $array['up_page'];
			$this -> down_page = $array['down_page'];
			$this -> last_page = $array['last_page'];
			$this -> style = $array['style'];
			$this -> sql = $array['sql'];
			
			return array('sql' => $this -> sql(), 'paging' => $this -> paging());
		}
		
		private function sql ()
		{
			$top = $this -> top;
			$page = $this -> page;
			$sql = $this -> sql;
			
			if (is_numeric($page)) {
				if ($page <= 0) {
					$page = 1;
				}
			} else {
				$page = 1;
			}
			
			$limit = $top * $page - $top;
			
			$sql = $sql . " limit $limit,$top";
			
			return $sql;
		
		}
		
		private function paging ()
		{
			$return = $this -> style;
			$return = str_replace('{home_page}', $this -> home_page(), $return);
			$return = str_replace('{up_page}', $this -> up_page(), $return);
			$return = str_replace('{paging_list}', $this -> paging_list(), $return);
			$return = str_replace('{down_page}', $this -> down_page(), $return);
			$return = str_replace('{last_page}', $this -> last_page(), $return);
			
			return $return;
		}
		
		private function paging_list ()
		{
			$total = $this -> total;
			$top = $this -> top;
			$page = $this -> page;
			$link = $this -> list_page;
			$now_link = $this -> list_page_now;
			$show = $this -> show;
			$list_b = floor($show / 2);
			$list_a = floor($show - $show / 2);
			
			$show % 2 == 0 && $list_a = $list_a -1;
			
			$page_num = ceil($total / $top);
			
			$page <= 0 && $page = 1;
			$page >= $page_num && $page = $page_num;
			
			if ($page_num > $show) {
				$new_page_num = $show;
				$b_page = $page - $list_b;
				$e_page = $new_page_num + ($page - $list_b) - 1;
				
				if ($b_page <= 0) {
					$b_page = 1;
					$e_page = $show;
				}
				if ($e_page >= $page_num) {
					$b_page = $b_page - $list_b + ($page_num - $page);
					$e_page = $page_num;
				}
			} else {
				$b_page = 1;
				$e_page = $page_num;
			}
			
			for ($i = $b_page; $i <= $e_page; $i++) {
				if ($page == $i) {
					$_link = str_replace('{page}', $i, $now_link);
				} else {
					$_link = str_replace('{page}', $i, $link);
				}
				$paging_list .= $_link;
			}
			
			return $paging_list;
		
		}
		
		private function home_page ()
		{
			$home_page = $this -> home_page;
			return str_replace('{page}', 1, $home_page);
		
		}
		
		private function up_page ()
		{
			$up_page = $this -> up_page;
			$page = $this -> page - 1;
			$page <= 0 && $page = 1;
			return str_replace('{page}', $page, $up_page);
		}
		
		private function down_page ()
		{
			$down_page = $this -> down_page;
			$page = ceil($this -> total / $this -> top);
			$this -> page <= 0 && $this -> page = 1;
			$page_d = $this -> page + 1;
			$page_d >= $page && $page_d = $page;
			return str_replace('{page}', $page_d, $down_page);
		}
		
		private function last_page ()
		{
			$last_page = $this -> last_page;
			$page = ceil($this -> total / $this -> top);
			return str_replace('{page}', $page, $last_page);
		}
		
	}//class end
?>